package com.sgb.member.view_model

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.sgb.member.MemberConfig
import com.sgb.member.entity.MemberCompanyTeamBean
import com.sgb.member.entity.MemberGoodsListEntity
import com.sgb.member.entity.MemberLeaseBean
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.util.PointUtils
import com.swgk.core.util.SharedPreferenceUtil

class MemberBusinessPriceModel : ViewModel() {
    var msgLiveData = MutableLiveData<String>()
    var isLoadLiveData = MutableLiveData<Boolean>()
    var isSuccess = MutableLiveData<Boolean>()

    //经营商品
    fun QuotePriceByBusiness(quotedCompanyNos: List<String>, mObject: Any, price: String) {
        val companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "")
        val map = HashMap<String, Any>()
        val entity = mObject as MemberGoodsListEntity
        val list = mutableListOf<Map<String, Any>>()
    //    for (no in quotedCompanyNos) {
            val map1 = HashMap<String, Any>().apply {
                put("brandId", entity.brandId)
                put("businessNo", entity.skuid)
                put("companyNo",companyNo)
                put("differencePrice", price.toDouble() - entity.price)
                put("firstCategoryNo", entity.cid1)
                put("secondCategoryNo", entity.cid2)
                put("thirdCategoryNo", entity.cid)
                put("itemId", entity.id)
                put("lastPrice", entity.quetePrice)
                put("originalPrice", entity.price)
                put("price", PointUtils.price(price.toDouble()))
                put("quotedCompanyNo", quotedCompanyNos[0])
                put("title", entity.cartTitle)
                put("unit", entity.unit)
            }
            list.add(map1)

     //   }
        map.put("companyNo", companyNo)
        map.put("quotedCompanyNos", quotedCompanyNos)
        map.put("goodsQuotePriceList", list)
        map.put("quotedType", 1)
        requestFun(map)
    }

    //租赁经营
    fun QuotePriceByRent(quotedCompanyNos: List<String>, mObject: Any, price: String) {
        val companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "")
        val map = HashMap<String, Any>()
        val entity = mObject as MemberLeaseBean
        val list = mutableListOf<Map<String, Any>>()
      //  for (no in quotedCompanyNos) {
            val map1 = HashMap<String, Any>().apply {
                put("businessNo", entity.no)
                put("businessType", entity.type)
                put("categoryName", entity.category)
                put("companyNo",companyNo)
                put("differencePrice", price.toDouble() - entity.price.toDouble())
                put("firstCategoryNo", entity.first_category)
                put("secondCategoryNo", entity.second_category)
                put("lastPrice", entity.quetePrice)
                put("originalPrice", entity.price)
                put("price", PointUtils.price(price.toDouble()))
                put("quotedCompanyNo", quotedCompanyNos[0])
                put("title", entity.title)
                put("unit", entity.priceUnit)
            }
            list.add(map1)
      //  }
        map.put("companyNo", companyNo)
        map.put("quotedCompanyNos", quotedCompanyNos)
        map.put("rentQuotePriceList", list)
        map.put("quotedType", 2)
        requestFun(map)
    }

    //租赁工程工队
    fun QuotePriceByTeam(quotedCompanyNos: List<String>, mObject: Any, price: String) {
        val companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "")
        val findBean = mObject as MemberCompanyTeamBean
        val map = HashMap<String, Any>()
        val list = mutableListOf<Map<String, Any>>()
      //  for (no in quotedCompanyNos) {
            val map1 = HashMap<String, Any>().apply {
                put("businessNo", findBean.no)
                put("companyNo",companyNo)
                put("differencePrice", price.toDouble() - findBean.price.toDouble())
                put("lastPrice", findBean.quetePrice)
                put("originalPrice", findBean.price.toDouble())
                put("price", PointUtils.price(price.toDouble()))
                put("quotedCompanyNo", quotedCompanyNos[0])
               // put("teamName", findBean.team_name)
                put("teamName", findBean.type_work_name)
                put("typeWork", findBean.type_work_name)
                put("unit", findBean.unit)
            }
            list.add(map1)
     //   }
        map.put("companyNo", companyNo)
        map.put("quotedCompanyNos", quotedCompanyNos)
        map.put("teamQuotePriceList", list)
        map.put("quotedType", 3)
        requestFun(map)
    }

    fun requestFun(map:Map<String, Any>){
        MemberConfig.getInstance().api.batchQuotePriceByType(map).enqueue(object : ICallBack<BaseEntity<BaseEntity<String?>?>?>() {
            override fun onSuccess(data: BaseEntity<BaseEntity<String?>?>?) {
                if (data?.isSuccess == true) {
                    msgLiveData.postValue("调价成功！")
                    isSuccess.postValue(true)
                }
            }
        })
    }

}